package cn.newgrand.netcall.imp.advertisement;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.jivesoftware.resource.Default;
import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.util.BrowserLauncher;
import org.jivesoftware.spark.util.GraphicUtils;

import com.sun.media.Log;

import cn.newgrand.netcall.advertisement.AdXML;
import cn.newgrand.netcall.advertisement.Advertisement;
import cn.newgrand.netcall.advertisement.AdvertisementPanel;

/**
 * 聊天窗体的底部广告类
 * 
 * @author SidneyRen
 * 
 */
public class AdChatBottom extends AdvertisementPanel {
	private List<Advertisement> adList = AdXML.getInstance()
			.getAdChatBottomList();
	private static int adNum = 0;
	private Image backgroundImage;
	private Advertisement ad;

	public AdChatBottom() {

		loadAd();

		getAdLabel().setForeground(Color.BLUE);

		add(getAdLabel(), new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(0, 8, 0, 0), 0, 0));
		
		final String str=getAdLabel().getText();
		getAdLabel().addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				getAdLabel().setCursor(GraphicUtils.HAND_CURSOR);
//				getAdLabel().setForeground(Color.RED);
				getAdLabel().setText("<html><u>"+str+"</u></html>");
			}

			public void mouseExited(MouseEvent e) {
				getAdLabel().setCursor(GraphicUtils.DEFAULT_CURSOR);
//				getAdLabel().setForeground(Color.BLUE);
				getAdLabel().setText(str);
			}
		});

	}

	/**
	 * 填充聊天窗口底部的文字广告 方法中调用父类中的fillAd方法来设置ad、刷新面板
	 * 
	 * @param ad
	 */

	public void fillChatBottomAd(Advertisement ad) {
		// getAdLabel().setText(ad.getTitle());
//		Log.error(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
//				.format(new Date())
//				+ "ad.title :" + ad.getSubtitle());
		
		getAdLabel().setText("新中大工程项目管理软件, 工程行业第一品牌");
		getAdLabel().setToolTipText("新中大工程项目管理软件, 工程行业第一品牌");
//		getAdLabel().setText("新中大金色快车GE，成长型企业整体解决方案");
//		getAdLabel().setToolTipText("新中大金色快车GE，成长型企业整体解决方案");
		
		
//		Log.error(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
//				.format(new Date())
//				+ "ad.title :" + ad.getSubtitle());
//		try {
//			getAdLabel().setToolTipText(new String(ad.getSubtitle().getBytes("gbk"),"UTF-8"));
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
		fillAd(ad);
	}

	public void loadAd() {
		if (adList.size() == 0)
			return;

		if (adList.size() > adNum)
			fillChatBottomAd(adList.get(adNum));
		else {
			adNum = 0;
			fillChatBottomAd(adList.get(adNum));
		}

	}

	/**
	 * 更新广告
	 * 
	 */
	public void updateAd() {
		if (adList.size() == 0)
			return;
		if (adList.size() > adNum) {
			fillChatBottomAd(adList.get(adNum));
			adNum += 1;
		} else {
			adNum = 0;
			fillChatBottomAd(adList.get(adNum));
		}

	}

}
